/*
 * FontPanel.java
 *
 * Created on 28.11.2008, 12:30:22
 */

package de.blitzcoder.collide.config.textarea;

import de.blitzcoder.collide.gui.textarea.CollIDETextArea;
import java.awt.GraphicsEnvironment;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author blitzcoder
 */
public class FontPanel extends javax.swing.JPanel implements ItemListener, ChangeListener {

    CollIDETextArea textArea;

    /** Creates new form FontPanel */
    public FontPanel(CollIDETextArea textArea) {
        this.textArea = textArea;
        initComponents();
        loadFonts();
        antiAliasCheckBox.addChangeListener(this);
        fontComboBox.addItemListener(this);
        fontSizeSpinner.addChangeListener(this);
        antiAliasCheckBox.setSelected(CollIDETextArea.getPropertyManager().getProperty("view.antiAlias").toLowerCase().equals("subpixel"));
    }

    private void loadFonts() {
        // Get the Font Family list
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fonts = env.getAvailableFontFamilyNames();

        // Put the FontFamilies in the ComboBox
        for (String f:fonts) {
            fontComboBox.addItem(f);
            if (CollIDETextArea.getPropertyManager().getProperty("view.font").equals(f))
                fontComboBox.setSelectedIndex(fontComboBox.getItemCount()-1);
        }

        fontSizeSpinner.setValue(new Integer(CollIDETextArea.getPropertyManager().getIntProperty("view.fontsize")));
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        fontComboBox = new javax.swing.JComboBox();
        fontSizeSpinner = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        antiAliasCheckBox = new javax.swing.JCheckBox();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Font"));
        setLayout(new java.awt.GridBagLayout());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(fontComboBox, gridBagConstraints);

        fontSizeSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(fontSizeSpinner, gridBagConstraints);

        jLabel1.setText("pt");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel1, gridBagConstraints);

        antiAliasCheckBox.setText("Kantenglättung");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(antiAliasCheckBox, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    private void valueChanged() {
        textArea.getPropertyManager().setProperty("view.font",(String)fontComboBox.getSelectedItem());
        textArea.getPropertyManager().setProperty("view.fontsize",""+(Integer)fontSizeSpinner.getValue());
        textArea.getPropertyManager().setProperty("view.antiAlias", antiAliasCheckBox.isSelected() ? "subpixel" : "none" );
        textArea.propertiesChanged();
    }

    public String getSelectedFont() {
        return (String)fontComboBox.getSelectedItem();
    }
    
    public int getSelectedFontSize() {
        return ((Integer)fontSizeSpinner.getValue()).intValue();
    }

    public boolean getAntiAliasEnabled() {
        return antiAliasCheckBox.isSelected();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox antiAliasCheckBox;
    private javax.swing.JComboBox fontComboBox;
    private javax.swing.JSpinner fontSizeSpinner;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void itemStateChanged(ItemEvent e) {
        valueChanged();
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        valueChanged();
    }

}
